/**
 * 
 */
package front2;

import java.sql.Timestamp;
import java.util.Scanner;

import be.StoredProcedures;
import be.db_backend;
import mt.middle;

/**
 * @author Yuuki
 * Contains the main function for the console front-end
 * should have methods for login since it should be done in the beginning of the session.
 *
 */
public class Login extends Commands {

	/**
	 * @param args
	 */
	static Root r;
	static String name,pass;
	static middle m;
	
	public static void main(String[] args) {
		Commands.s = new Scanner(System.in);
		//m = new middle(new db_backend());
		m = new middle(new StoredProcedures());
		
		while(true){
			String command = readCommand("what do you want to do? register - guest - login");
			if(command.equals("register")){
				String name = Commands.readCommand("User: ");
				String email = Commands.readCommand("email: ");
				String pass = Commands.readCommand("password: ");
				m.CreateUser(0, 0, name, email, pass, new Timestamp(System.currentTimeMillis()), 0);
				
			}else if(command.equals("guest")){
				userID=-1;
				r = new Root(s);
				r.run();
			}else if(command.equals("login")){
				System.out.println("Login to the forum with your username and password!");
				name = Commands.readCommand("User: ");
				pass = Commands.readCommand("password: ");
				if(login(name,pass)){
					userID = m.getUser(name).getUserID();
					r = new Root(s);
					r.run();
				}
			}else{
				System.out.println("unknown command: " + command);
			}
		}
	}
	/**
	 * should check if username + password matches if it does return true else false
	 * @param name the login name of the user
	 * @param pass the password corresponding to the username
	 * @return true if there is a match in the database else false
	 */
	public static boolean login(String name,String pass){
		return m.login(name, pass);
	}
	/**
	 * constructor maybe not needed
	 */
	public Login(){
		
	}
}
